/*#include <REGX52.H>

void init()
{
	PCON |= 0x80;
	SCON = 0x50;
	TMOD &= 0x0F;
	TMOD |= 0x20;
	TL1 = 0xF4;   //must
	TH1 = 0xF4;
	ET1 = 0;
	TR1 = 1;	
}

//i'am about to transmission information
void stran(char a)
{
	SBUF = a;
	while(TI == 0);
	TI = 0;
}


int main()
{
	int a = 0;
	init();
	for(a = 0;a < 10; a++)
	{init();
		stran(a + '0');
		stran(' ');
	}
	stran('\n');
	while(1);
	return 0;
}*/


//calculation triangle'fun;
#include <REGX52.H>
#include <intrins.h>
#include<lcd1602.h>

//send the information computer

void send_init()
{
	PCON |= 0x80;
	SCON = 0x50;
	TMOD &= 0x0F;
	TMOD |= 0x20;
	TL1 = 0xF4;
	TH1 = 0xF4;
	ET1 = 0;
	TR1 = 1;
}

void send_char(char a)
{
	SBUF = a;
	while(TI == 0);
	TI = 0;
}

//time delay
void time(int a)
{
	while(a--)
	{
		_nop_();
		_nop_();
		_nop_();
	}
}

//i'm going to read keyboard;
/*
int key()
{
	
	char a = 0;
	char b = 100;
	//judge column
	P1 = 0x0f;
	if(P1_0 == 0)
	{
		a = 4;
		LCD_ShowNum(2,1,0,1);
	}
	if(P1_1 == 0)
	{
		a = 3;
		LCD_ShowNum(2,1,1,1);
	}
	if(P1_2 == 0)
	{
		a = 2;
		LCD_ShowNum(2,1,2,1);
	}	
	if(P1_3 == 0)
	{
		a = 1;
		LCD_ShowNum(2,1,3,1);
	}
	time(10);
	
	//judge line
	P1 = 0xf0;
	if(P1_4 == 0)
	{
		a = a + 12;
		LCD_ShowNum(2,3,4,1);
	}
	if(P1_5 == 0)
	{
		a = a + 8;
		LCD_ShowNum(2,3,3,1);
	}
	if(P1_6 == 0)
	{
		a = a + 4;
		LCD_ShowNum(2,3,2,1);
	}
	if(P1_7 == 0)
	{
		LCD_ShowNum(2,3,1,1);
	}
	while(b--&&P1 != 0xf0)
	{
		time(b*100);
	}
	if(b == 0)
		return 0;
	return a;
}*/

char key()
{
	char b = 0;
	char a = 0;
	P1 = 0x0f;
	if(P1_0 == 0)
		a = 4;
	else if(P1_1 == 0)
		a = 3;
	else if(P1_2 == 0)
		a = 2;
	else if(P1_3 == 0)
		a = 1;
	else 
		return 0;
	time(1);
	P1 = 0xf0;
	if(P1_4 == 0)
		a += 12;
	else if(P1_5 == 0)
		a += 8;
	else if(P1_6 == 0)
		a += 4;
	
	while(b < 50 && P1 != 0xf0)
	{
		time(1000);
		b++;
	}
	time(1000);
	
	if(b >= 50)
		return 0;
	return a;
}    
 


int main()
{
	char a = 0;
	LCD_Init();
	send_init();

	while(2)
	{
		a = key();
		if(a == 0)
			continue;
		send_char(a + '0');
		LCD_Init();
		LCD_ShowNum(1,1,a,1); 
	}
	
	return 0;
}